/// @tags:
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#include <unordered_map>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

using ll = long long;

ll qpow(ll base, int exp, int mod) {
  ll res = 1;
  while (exp) {
    if (exp & 1) res = res * base % mod;
    base = base * base % mod;
    exp >>= 1;
  }
  return res;
}

ll bsgs(ll a, ll b, ll p) {
  static std::unordered_map<ll, ll> has;
  ll blo = (ll)sqrt(p) + 1;
  ll q;
  b = qpow(b, p - 2, p);
  q = qpow(a, blo, p);
  q = qpow(q, p - 2, p);
  for (ll i = 1, j = 0; j <= blo; ++j, i = i * a % p) {
    has[i] = j;
  }
  for (ll i = b, j = 0; j <= blo; ++j, i = i * q % p) {
    if (has.find(i) != has.end()) return j * blo + has[i];
  }
  return -1;
}

int main() {
  ll a, b, m, x0, x;
  cin >> a >> b >> m >> x0 >> x;
  if (x0 == x) {
    cout << "YES";
    return 0;
  }
  if (a == 0) {
    if (x == b) {
      cout << "YES";
    } else {
      cout << "NO";
    }
    return 0;
  }
  if (a == 1 && b == 0) {
    cout << "NO";
    return 0;
  }
  a -= 1;
  if (a < 0) a += m;
  ll tofind = (a * x % m + b) % m * qpow((a * x0 % m + b) % m, m - 2, m) % m;
  a += 1;
  if (a >= m) a -= m;
  if (bsgs(a, tofind, m) != -1) {
    cout << "YES\n";
  } else {
    cout << "NO\n";
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("C. Random Number Generator.in", "r", stdin);
  freopen("C. Random Number Generator.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
